app.controller('flowerController', function($scope, $controller, baseService) {
    // 继承基础控制器
    $controller('baseController', {$scope: $scope});



    // 1.多条件查询
    $scope.search=function (page,rows) {
        baseService.findByPage("/flower/findByPage",page,rows).then(function (value) {
            $scope.dataList = value.data.list;
            $scope.paginationConf.totalItems= value.data.totalItems;//设置分页总数
        })

    }

    // $scope.brandList ={data:[{id:1,text:"玫瑰"},{id:3,text:"百合"},{id:4,text:"牡丹"}]};

    // 2.查询所有的鲜花种类
    $scope.findAllCategory=function () {
        baseService.sendGet("/catalog/findAll").then(function (response) {
            $scope.brandList ={data:response.data}
        })
    }

    // 3.上传鲜花图片
    $scope.flower={urlList:[],cId:""}
    $scope.uploadFile=function () {
        baseService.uploadFile().then(function(response) {
            $scope.images=response.data;
            if($scope.images.length>0){
                for (var i=0;i< $scope.images.length;i++){
                    $scope.flower.urlList.push($scope.images[i])
                }
            }
        });
    }

    // 4.删除图片

    $scope.deleteImg=function (index) {
        $scope.flower.urlList.splice(index,1);
    }

    // 5封装鲜花分类id
    $scope.saveCid=function () {
        $scope.flower.cid=$scope.category.id
        $scope.flower.catalog.cid=$scope.category.id;

    }

    // 6.回显数据
    $scope.show = function(entity) {
        /** 把json对象转化成一个新的json对象 */
        $scope.flower = JSON.parse(JSON.stringify(entity));
        $scope.category={id:entity.catalog.cid,text:entity.catalog.cname}

        // 异步请求查询鲜花图片
          baseService.sendGet("/flower/findByfid","fid="+entity.fid).then(function (response) {
              $scope.flower.urlList=response.data;
          })

    };

    // 7.添加或修改
    $scope.updateOrSave=function (flower) {
        var url="save"
        if(flower.fid){
            url="update"
        }
        baseService.sendPost("/flower/"+url,$scope.flower).then(function (response) {
            if (response.data) {
                alert("添加或更新成功")
                $scope.reload();


            }else {
                alert("添加或更新失败")
            }
        })

    }

    // 8.根据id批量删除
    $scope.deleteByid=function () {
        alert($scope.ids)

        baseService.deleteById("/flower/delete",  $scope.ids).then(function (response) {
            if (response.data) {
                alert("删除成功")
                // 重新加载
                $scope.reload();
            }else {
                alert("删除失败")
            }

        })
    }

})